from flask import request

from handler.json_response import *

from config.BaseApp import create_app

app = create_app()


@app.errorhandler(Exception)
def error_handler(e):
    """
    全局异常捕获
    """
    print(str(e))
    app.logger.info(str(e))
    retResponse = JsonResponse.error(code=500, msg=str(e))
    return JsonResponse.to_json(retResponse)


# 请求进入拦截 对应 after_request 正常返回否则异常就被 errorhandler拦截处理
@app.before_request
def pre_request():
    app.logger.debug(f'path：{request.path}')
    app.logger.debug(f'args：{dict(request.args)}')
    app.logger.debug(f'headers：{request.headers}')
    app.logger.debug(f'cookies：{dict(request.cookies)}')
    #app.logger.debug(f'json：{request.json}')


if __name__ == '__main__':
    app.run()
